<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Email_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getMyEmailList($fields = ' * ',$per_page, $limit)
    {
    	
    	$limit = " limit {$per_page},{$limit}";

        $sql = "SELECT {$fields} FROM tr_message_push_map m , tr_emailsender e  WHERE m.typeid=e.id AND m.to_userid = {$_SESSION['userInfo']['AccountID']} AND m.isDel=0  ORDER BY m.createtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }


    public function getMyEmailListCnt($isRead = '')
    {
        $where = " ";
        if(!empty($isRead)){
            $where .= " AND m.readtime =0 ";
        }
        $sql = "SELECT m.id FROM tr_message_push_map m , tr_emailsender e  WHERE m.typeid=e.id AND m.isDel=0 AND m.to_userid = {$_SESSION['userInfo']['AccountID']} $where";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

    public function delEmail($id,$to_userid){
    	if(empty($id) || empty($to_userid)){
    		return false;
    	}
        $this->db->where(["id"=>$id,"to_userid"=>$to_userid]);
        $res = $this->db->update("tr_message_push_map",['isDel'=>1]);
        return $res;
    }

    public function getDetail($fields = ' * ',$id = 0)
    {
    	$sql = "SELECT {$fields} FROM tr_message_push_map m , tr_emailsender e  WHERE m.typeid=e.id AND m.to_userid = {$_SESSION['userInfo']['AccountID']} AND m.isDel=0 AND e.id={$id} ";
    	$query = $this->db->query($sql);
    	return $query->row_array();
    }

    /**
     * 设置已读状态
    */
   public function setRead($id = 0){
   		if(empty($id))return false;
   		$this->db->where(['id'=>$id,'to_userid'=>$_SESSION['userInfo']['AccountID'],'readtime'=>0]);
   		$res = $this->db->update("tr_message_push_map",['readtime'=>time()]);
   		return $res;
   }
   
    
}

/* End of file Index_model.php */
/* Location: ./application/models/index_model.php */